package com.crawler.repitle;

import com.crawler.repitle.serialize.Record;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

public class AvroUtil {
    public byte[] generateAvro(Map<String, String> entry) {
        Record record = new Record();
        record.setJobName(entry.get("jobName"));

        byte[] result = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
            DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(Record.getClassSchema());
            writer.write(record, encoder);
            encoder.flush();
            result = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
